<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'main';
if($_RQ['op']=='main'){
	//这里整理跳转到游戏页面
	$return['gameset']=$this->plugin_setting('game');
	$game=pdo_get('jy_weishop_game',array('action'=>'jy_robshop/game/','plugin_name'=>'jy_robshop','uniacid'=>$_SESSION['uniacid']));
	$id=$game['id'];
	$game['pluginset']=$game['pluginset']?json_decode($game['pluginset'],true):array('rewardtype'=>0);
	if($game['merchid']){
		$sender=pdo_get('jy_merch_user',array('id'=>$game['merchid'],'uniacid'=>$_SESSION['uniacid']), array('merchname','logo','desc'));
		$sender['nickname']=$sender['merchname'];
		$sender['avatar']=$sender['logo'];
	}elseif($game['mid']){
		$sender=pdo_get('core_members',array('id'=>$game['mid'],'uniacid'=>$_SESSION['uniacid']), array('nickname', 'avatar'));
	}else{
		$sender=array(
			'nickname'=>$_SESSION['account']['title'],
			'avatar'=>$_SESSION['account']['logo'],
		);
	}
	$game['lastnum']=model('jy_weishop/game')->check_num($id);
	$game['canyu_num']=model('jy_weishop/game')->canyu_num($id);
	$logs=model('jy_weishop/game')->get_log($id);
	$marqueetip='';
	foreach($logs as $lv){
		$nickname=pdo_getcolumn('core_members',array('id'=>$lv['mid'],'uniacid'=>$_SESSION['uniacid']), 'nickname');
		$time=date('Y-m-d H:i:s',$lv['createtime']);
		$lv['reward']=json_decode($lv['reward'],true);
		$reward_name=$lv['reward']['title'];
		$marqueetip .=$nickname.'[抽到《'.$reward_name.'》,时间:'.$time.']；';
	}
	$game['marqueetip']=$marqueetip;
	$game['reward']=json_decode($game['reward'],true);
	foreach($game['reward'] as &$av){
		$av['image']=tomedia($av['image']);
	}
	$return['game']=$game;
	$return['sender']=$sender;
	$return['list']=array();
	$return['myredfee']=pdo_getcolumn('jy_weishop_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']), 'redfee');
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','mobile','avatar','nickname'));
	$member['id']=$_SESSION['uid'];
	$return['member']=$member;
	$return['sharedata']=array(
		'title'=> '来大转盘抽奖啦！！！',
		'desc'=> $game['title'],
		'link'=>custom_url('jy_bigwheel/game//id='.$id.'&fromuser='.$_SESSION['uid'],true),
		'imgUrl'=> tomedia('../addons/jy_bigwheel/images/red_rain.png'),
	);
	exi($return);
}
if($_RQ['op']=='getlog'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
    $orderby='createtime desc';
	$total='';
	$gameids=pdo_getall('jy_weishop_game',array('plugin_name'=>'jy_robshop','uniacid'=>$_SESSION['uniacid']),'id','id');
	if($gameids){
		$gameids=array_keys($gameids);
	}else{
		$gameids=-1;
	}
	$params['game_id']=$gameids;
	$return['list']=pdo_getslice('jy_weishop_game_getlog',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
			$v['createtime']=date('Y-m-d H:m:s',$v['createtime']);
			$v['reward']=json_decode($v['reward'],true);
			$v['typetitle']=$this->prizeType[$v['reward']['type']];
			$v['fee']=$v['fee']>0?$v['fee']:$v['reward']['jifen'];
			if($v['reward']['type']==1){
				$v['fee']=$v['reward']['title'];
			}
			$v['member']=cfc('core')->member_show($v['mid']);
	}
	exi($return);
}
if($_RQ['op']=='getreward'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$res=$this->model('game')->get_choujiang($id);
	exi($res);
}
if($_RQ['op']=='reward'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$reward_id=intval($post['reward_id']);
	$log_id=intval($post['log_id']);
	$res=$this->model('game')->get_reward($id,$reward_id,$log_id);
	exi($res);
}
if($_RQ['op']=='set_reward'){
	$post=get_postdata();	
	$id=intval($post['id']);
	$log=pdo_get('jy_weishop_game_getlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$reward=json_decode($log['reward'],true);
	$game=pdo_get('jy_weishop_game',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['game_id']));
	$res=model('jy_weishop/game')->set_reward($reward,$log['mid'],$game,$id,true);
	exi('领取成功！');
}